package com.ityang.hhhh.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ityang.hhhh.conm.BaseContext;
import com.ityang.hhhh.conm.R;
import com.ityang.hhhh.domain.ShoppingCart;
import com.ityang.hhhh.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车
 */
@RestController
@RequestMapping("/ShoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;


    /**
     * 添加购物车
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        log.info("购物车数据:{}", shoppingCart);

        //设置用户id，指定当前是哪个用户的购物车数据
        Long currentId = shoppingCart.getUserId();
        shoppingCart.setUserId(currentId);

        Long shoppingId = shoppingCart.getShoppingId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, currentId);

        if (shoppingId != null) {
            //添加到购物车的是商品
            queryWrapper.eq(ShoppingCart::getShoppingId, shoppingId);

        }

        //查询当前商品是否在购物车中
        //SQL:select * from shopping_cart where user_id = ? and dish_id/setmeal_id = ?
        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);

        if (cartServiceOne != null) {
            //如果已经存在，就在原来数量基础上加一
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number + 1);
            shoppingCartService.updateById(cartServiceOne);
        } else {
            //如果不存在，则添加到购物车，数量默认就是一
            //shoppingCart.setNumber(1);

            shoppingCartService.save(shoppingCart);
            cartServiceOne = shoppingCart;
        }

        return R.success(cartServiceOne);
    }


    /**
     * 根据用户id查询购物车
     *
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(Long id) {
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId,id);


        List<ShoppingCart> list = shoppingCartService.list(lambdaQueryWrapper);

        return R.success(list);
    }


    /**
     * 清除购物车
     *
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(Long userId) {
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId, userId);

        shoppingCartService.remove(lambdaQueryWrapper);
        return R.success("清空购物车成功");
    }

    /**
     * 移除购物车
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart) {
        //设置用户id，指定当前是哪个用户的购物车数据
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        Long ShoppingId = shoppingCart.getShoppingId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, currentId);

        if (ShoppingId != null) {
            //添加到购物车的是商品
            queryWrapper.eq(ShoppingCart::getShoppingId, ShoppingId);

        }
            //查询当前商品是否在购物车中
            //SQL:delete  from shopping_cart where user_id = ? and dish_id/setmeal_id = ?
            ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);

            if (cartServiceOne != null) {
                //如果已经存在，就在原来数量基础上加一
                Integer number = cartServiceOne.getNumber();
                cartServiceOne.setNumber(number - 1);
                shoppingCartService.updateById(cartServiceOne);
            } else {
                //如果不存在，则添加到购物车，数量默认就是
                return R.error("请先添加到购物车");
            }

            return R.success(cartServiceOne);
        }

}
